package com.wyz.module.user.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyz.module.user.mapper.UserMapper;
import com.wyz.module.user.model.convert.UserConvert;
import com.wyz.module.user.model.dto.UserDTO;
import com.wyz.module.comm.entity.UserDO;
import com.wyz.module.user.model.vo.UserVO;
import com.wyz.module.user.service.UserService;
import org.springframework.stereotype.Service;
import com.wyz.module.common.util.ConvertUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 无涯子
 * @version 1.0
 * @date 2024/4/14 17:57:04
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserConvert userConvert;

    /***
     * 根据ID查询用户信息
     * @param id ID
     * @return 用户信息
     */
    @Override
    public UserVO getUserById(Integer id) {
        return userConvert.toVO(userMapper.selectById(id));
    }

    /***
     * 查询所有用户信息
     * @return 所有用户信息
     */
    @Override
    public List<UserVO> getUserAll() {
        return userConvert.toListVO(userMapper.selectList(null));
    }

    /***
     * 新增用户信息
     * @param userDTO 用户信息
     * @throws RuntimeException 异常信息
     */
    @Override
    public void saveUser(UserDTO userDTO) throws RuntimeException{
        UserDO user = userConvert.toDO(userDTO);
        int insert = userMapper.insert(user);
        if(insert != 1){
            throw new RuntimeException("保存用户信息失败");
        }
    }

    /***
     * 更新用户信息
     * @param userDTO 用户信息
     * @throws RuntimeException 异常信息
     */
    @Override
    public void updateUser(UserDTO userDTO) throws RuntimeException {
        UserDO user = userConvert.toDO(userDTO);
        int i = userMapper.updateById(user);
        if(i != 1){
            throw new RuntimeException("更新用户信息失败");
        }
    }

    /***
     * 作废用户信息
     * @param id 用户ID
     * @throws RuntimeException 异常信息
     */
    @Override
    public void removeUserById(Integer id) throws RuntimeException {
        int i = userMapper.deleteById(id);
        if(i != 1){
            throw new RuntimeException("作废用户信息失败");
        }
    }

    /***
     * 分页查询
     * @param current 当前页
     * @param size  页大小
     * @return 分页数据
     */
    @Override
    public Page<UserVO> getPage(Long current, Long size) {
        Page<UserDO> userDoPage = new Page<>();
        userDoPage.setCurrent(current);
        userDoPage.setSize(size);
        Page<UserDO> page = userMapper.selectPage(userDoPage, null);
        return ConvertUtil.convert(page, userConvert::toVO);
    }
}
